import { _decorator, Component, director, Label, Node, ProgressBar, Size, sys } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LoadingController')
export class LoadingController extends Component {

    @property({ type: ProgressBar })
    public progressBar: Node | null = null; // 启动进度条
    @property({ type: Label })
    public barLabel: Label | null = null; // 进度条文本

    private progress: number = 0;

    onLoad() {
        // 进度条初始值
        this.progressBar.getComponent(ProgressBar).progress = 0;
    }

    start() {
        this.launchGame();
    }
    parseLaunchParams() {
        console.log("解析启动参数");
        // 在这部分解析你的启动参数，注意，这里的实现会根据平台的不同而有不同的处理方式
        // 示例中，我们只是简单地输出信息
        this.barLabel.string = "解析启动参数"
    }
    getDeviceInfo() {
        console.log("获取设备信息");
        // 获取并打印出屏幕大小
        let screenSize: Size = sys.windowPixelResolution;
        console.log(`屏幕大小：宽=${screenSize.width}, 高=${screenSize.height}`);
        // 这里可以根据需要获取更多的设备信息，例如平台、操作系统等
    }
    fetchConfig() {
        return new Promise((resolve) => {
            console.log("获取游戏配置");
            this.barLabel.string = "获取游戏配置"
            setTimeout(() => resolve(true), 1000); // 延迟模拟
        });
    }
    fetchResVersion(){
        this.barLabel.string = "获取资源版本"
        return new Promise((resolve) => {
            console.log("获取资源版本");
            setTimeout(() => resolve(true), 500); // 延迟模拟
        });
    }
    updateResources() {
        this.barLabel.string = "更新资源"
        return new Promise((resolve) => {
            console.log("更新资源");
            setTimeout(() => resolve(true), 500); // 延迟模拟
        });
    }
    login() {
        this.barLabel.string = "登录成功"
        return new Promise((resolve) => {
            console.log("登录成功");
            setTimeout(() => resolve(true), 500); // 延迟模拟
        });
    }
  
    enterGame() {
        // 加载主场景
        director.loadScene("game");
    }
    
    async launchGame() {
        console.log("启动游戏");
        // 解析启动参数
        this.parseLaunchParams();
        // 获取设备信息
        this.getDeviceInfo();
        this.progress += 0.2;
        let res1 = await Promise.all([this.fetchConfig(),this.fetchResVersion()]);
        console.log(res1);
        if(!res1[0] || !res1[1]){
            return;
        }
        this.progress += 0.3;
        // 更新资源
        let res2 = await this.updateResources();
        if(!res2){
            return;
        }
        this.progress += 0.3;
        await this.login();
        this.progress += 0.2;
        // 获取完毕，进入游戏
        this.enterGame();
    }

    protected update(dt: number): void {
        this.progressBar.getComponent(ProgressBar).progress = this.progress;
    }
       
}

